package top.mataiji.manager.controller;

import mataiji.config.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import top.mataiji.manager.pojo.Book;
import top.mataiji.manager.pojo.Bookimg;
import top.mataiji.manager.pojo.dtd.BookImgDtd;
import top.mataiji.manager.pojo.vo.BookVo;
import top.mataiji.manager.service.BookImgService;
import top.mataiji.manager.service.BookService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zhaoss
 * @Date: 2021/3/20 - 03 - 20 - 11:00
 * @Description: top.mataiji.manager.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("book")
public class BookController {

    @Autowired
    BookService bookService;
    @Autowired
    BookImgService bookImgService;

    /**
     * 查询所有书籍
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public List<BookVo> findAllBook(){
        System.out.println(bookService.findAllBook());
        return bookService.findAllBook();
    }

    /**
     * 查询商品详情
     * @param bookId
     * @return
     */
    @RequestMapping("findBookById")
    @ResponseBody
    public BookVo findBookById(Integer bookId){
        return bookService.findBookBiId(bookId);
    }

    @Transactional()
    @RequestMapping("addBook")
    @ResponseBody
    public Result addBook(Book book, BookImgDtd bookImgDtd) {
        Integer bookId = book.getBookid();
        int result = 0;
        System.out.println(book+" "+bookImgDtd);
        //添加书籍
        System.out.println(book);
        //插入书籍,并获取bookId
        if(book.getBookid()!=null&&book.getBookid()!=0){
            result = bookService.alertBook(book);
        }
        else{
            System.out.println(book.getBookid()+"----------");
            bookId = bookService.addBook(book);
            book.setBookid(0);
        }
        System.out.println(bookId);
        //创建BookImg对象
        List<Bookimg> bookImgs = new ArrayList<>();
        Bookimg bookImg = null;
        List<String> bookimgs = bookImgDtd.getBookimg();
        List<Integer> id = bookImgDtd.getId();
        int i=0;
        for (String url: bookimgs) {
            bookImg = new Bookimg();
            if(id!=null&&!id.isEmpty()){
                bookImg.setId(id.get(i++));
            }
            bookImg.setBookid(bookId);
            bookImg.setBookimageaddress(url);//设置图片地址
            bookImgs.add(bookImg);
        }
        System.out.println(bookImgs);
        if(book.getBookid()!=null&&book.getBookid()!=0){
            System.out.println(book.getBookid()+"=====================");
            result =  bookImgService.alertBook(bookImgs);
        }else{
            //添加书籍图片
            result = bookImgService.addBookImg(bookImgs);
        }
        System.out.println(bookId+" "+result);
        if(bookId!=0&&result!=0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @RequestMapping("alertBook")
    public Result alertBook(Book book, BookImgDtd bookimg){
//        System.out.println(book+" "+bookimg);

        return Result.error("修改失败");
    }

    @RequestMapping("selectBook")
    @ResponseBody
    public List<BookVo> selectByName(String name){
        return bookService.seleteByBook(name);
    }

    @RequestMapping("selectBookByType")
    public List<BookVo> seleteBookByType(Integer name){
        return bookService.selectByType(name);
    }

}
